let token = getToken();
let user = JSON.parse(window.localStorage.getItem("userInfo"));
let payInfo;
let payType = null;
const payTypes = [
  {
    text: "支付宝",
    value: "ZFB",
  },
  {
    text: "微信",
    value: "WX",
  },
  {
    text: "银行卡",
    value: "YHK",
  },
];
function startRead() {
  var fileDom = document.getElementById("imgUpFile");
  var img = document.getElementById("imgContentImg");
  if (fileDom && img) {
    fileHandle(fileDom, img);
  }
}

$("#payMethod").click(function () {
  var picker = new mui.PopPicker();
  picker.setData(payTypes);
  picker.show(function (selectItems) {
    $("#payMethod").val(selectItems[0].text);
    payType = selectItems[0].value;
  });
});

function fileHandle(fileDom, img) {
  let codeStr = randomString(8);
  var file = fileDom.files[0];
  if(file.size>5242880){
    mui.alert("图片不得大于5M");
    return
  }
  var formFile = new FormData();
  formFile.append("file", file);
  console.log(formFile);
  var reader = new FileReader();
  reader.readAsDataURL(file);
  reader.onloadstart = function (e) {
    $.ajax({
      url: `${server}/v1/img/upload?project=${codeStr}`,
      data: formFile,
      dataType: "json",
      type: "post",
      processData: false, // 使用formData传参很重要的配置
      contentType: false, // 使用formData传参很重要的配置
      headers: { token: token },
      success: function (res) {
        console.log(res);
        if (res.code == 200) {
          console.log(res.data.data.src);
          payInfo = res.data.data.src;
          $("#pay").val(res.data.data.src);
        } else {
          mui.alert(res.msg);
        }
      },
      error: function (err) {
        alert(JSON.stringify(err))
        mui.alert("服务器出了点小问题，请稍后重试");
      },
    });
  };
  reader.onload = function (e) {
    // var param = {
    //   keyst: codeStr,
    //   val: reader.result.split('base64,')[1],
    // }
    // $.ajax({
    //   url: `${server}/v1/api/diction`,
    //   data: JSON.stringify(param),
    //   dataType: "json",
    //   type: "post",
    //   headers: {
    //     "Accept": "application/json",
    //     "Content-Type": "application/json",
    //     token: token
    //   },
    //   processData: false,
    //   success: function (res) {
    //     console.log(res);
    //     if (res.code == 200) {
    //       // console.log(res.data.data.src);
    //       // payInfo = res.data.data.src;
    //       // $("#pay").val(res.data.data.src);
    //     } else {
    //       mui.alert(res.msg);
    //     }
    //   },
    //   error: function (err) {
    //     mui.alert("服务器出了点小问题，请稍后重试");
    //   },
    // });
    img.setAttribute("src", reader.result);
  };
}
function randomString(e) {
  e = e || 32;
  var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
    a = t.length,
    n = "";
  for (i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
  return n;
}

$("#SendLetter").click(function () {
  let userSite = {
    payName: $("#payName").val(),
    pay: payInfo,
    type: payType,
    userId: user.id,
  };
  if (!payInfo) {
    mui.alert(`请先上传${payTypes.find((i) => i.value === payType).text}`);
    return false;
  }
  $.ajax({
    url: `${server}v1/api/blance/site`,
    data: JSON.stringify(userSite),
    dataType: "json",
    type: "post",
    processData: false,
    headers: { "Content-Type": "application/json", token: token },
    success: function (res) {
      if (res.code == 200) {
        mui.alert("添加成功!");
        setTimeout(() => {
          // location.href='memberCenter.html'
        }, 1000);
      } else {
        mui.alert(res.msg);
      }
    },
    error: function (err) {
      mui.alert("服务器出了点小问题，请稍后重试");
    },
  });
});
